
import { config } from '../../config/index';
import { getSelectedSchoolId, getUserId, setSelectedSchoolId, getSelectedSchoolData, setSelectedSchoolData } from '../../userData/userData';



/** 获取学校年级 */
function mockFetchDeliveryGrades(school, stage) {
  const { delay } = require('../_utils/delay');
  const { genGrades } = require('../../model/students');

  return delay().then(() => genGrades(school, stage));
}
/** 获取年级 */
export function fetchDeliveryGrades(school, stage) {
  if (config.useMock) {
    return mockFetchDeliveryGrades(school, stage);
  }

  return new Promise((resolve) => {
    resolve('real api');
  });
}


/*设置默认子女列表*/
export function setDefaultStudentList(id, schoolId, callBack) {
  const { setSelectedStudentId, getSelectedStudentId } = require('../../userData/userData');
  setSelectedStudentId(id)
  setSelectedSchoolId(schoolId)
  getSchoolFromId(schoolId).then((result) => {
    setSelectedSchoolData(result.result[0])
    if (callBack)
      callBack()
  })
}

/** 添加子女 */
export function addStudent(data) {
  console.log(data)
  return wx.cloud.callFunction({
    name: "user_addStudent",
    data: {
      u_id: getUserId(),
      i_id: data.i_id || getSelectedSchoolData()._id || 0,
      name: data.name,
      sex: data.sex,
      height: data.height,
      weight: data.weight,
      grade: data.grade,
      class: data.class,
      special: data.special == 1 ? 1 : 0,
      bust: data.bust,
      waist: data.waist,
      hip: data.hip,
      default: data.default
    }
  })
}

/** 更新子女 */
export function updateStudent(data) {
  console.log(data)
  return wx.cloud.callFunction({
    name: "user_updateStudent",
    data: {
      student_id: data._id,
      u_id: getUserId(),
      i_id: getSelectedSchoolId(),
      name: data.name,
      sex: data.sex,
      height: data.height,
      weight: data.height,
      grade: data.grade,
      class: data.class,
      special: data.special == 1 ? 1 : 0,
      bust: data.bust,
      waist: data.waist,
      hip: data.hip,
      default: data.default
    }
  })
}
//删除子女
export function deleteStudent(id) {
  return wx.cloud.callFunction({
    name: "user_deleteStudent",
    data: {
      student_id: id,
    }
  })
}
//获取所有学生
export function getAllStudent() {
  return wx.cloud.callFunction({
    name: "user_getAllStu",
    data: {
      user_id: getUserId()
    }
  })
}

//设置默认学生
export function setDefaultStudent(id) {
  const userId = getUserId()
  return wx.cloud.callFunction({
    name: "user_switchDefaultStu",
    data: {
      user_id: userId,
      default_student_id: id
    }
  })
}
//获取当前code学校
export function getSchoolFromCode(id) {
  return wx.cloud.callFunction({
    name: "institution_search",
    data: {
      key: "code",
      value: id
    }
  })
}
//获取当前id学校
export function getSchoolFromId(id) {
  return wx.cloud.callFunction({
    name: "institution_search",
    data: {
      key: "_id",
      value: id
    }
  })
}
//获取所有有学生的学校
export function getAllSchool() {
  return wx.cloud.callFunction({
    name: "user_getAllInstitution",
    data: {
      user_id: getUserId()
    }
  })
}

//获取所有学校
export function getAllSchools() {
  return wx.cloud.callFunction({
    name: "institution_getAll",
  })

}


